/**
 * API接口统一管理
 * 按模块组织所有API接口
 */

import { get, post, put, del, uploadFile, downloadFile } from '@/utils/request.js'

/**
 * 认证相关API
 */
export const authAPI = {
  /**
   * 用户登录
   * @param {Object} data 登录数据
   * @returns {Promise}
   */
  login: (data) => post('/auth/operator/login', data),
  
  /**
   * 用户登出
   * @returns {Promise}
   */
  logout: () => del('/auth/operator/logout'),
  
  /**
   * 刷新token
   * @returns {Promise}
   */
  refreshToken: () => post('/auth/operator/refresh'),
  
  /**
   * 获取用户信息
   * @returns {Promise}
   */
  getUserInfo: () => get('/auth/userinfo'),
  
  /**
   * 修改密码
   * @param {Object} data 密码数据
   * @returns {Promise}
   */
  changePassword: (data) => post('/auth/change-password', data)
}

/**
 * 电柜管理相关API
 */
export const cabinetAPI = {
  /**
   * 获取电柜列表
   * @param {Object} params 查询参数
   * @returns {Promise}
   */
  getCabinetList: (data) => post('/operator/maintenance/getCabinetList', data),
  
  /**
   * 获取电柜详情
   * @param {String|Number} id 电柜ID
   * @returns {Promise}
   */
  getCabinetDetail: (params = {}) => get(`/operator/maintenance/getCabinetDoorList`, params),
  
  /**
   * 添加电柜
   * @param {Object} data 电柜数据
   * @returns {Promise}
   */
  addCabinet: (data) => post('/operator/maintenance/addCabinet', data),
  
  /**
   * 更新电柜信息
   * @param {String|Number} id 电柜ID
   * @param {Object} data 更新数据
   * @returns {Promise}
   */
  updateCabinet: (data) => post(`/operator/maintenance/editCabinet`, data),
  
  /**
   * 删除电柜
   * @param {String|Number} id 电柜ID
   * @returns {Promise}
   */
  deleteCabinet: (id) => del(`/cabinets/${id}`),
  
  /**
   * 批量操作电柜
   * @param {Object} data 操作数据
   * @returns {Promise}
   */
  batchOperate: (data) => post('/cabinets/batch', data),
  
  /**
   * 获取电柜统计信息
   * @returns {Promise}
   */
  getCabinetStats: () => get('/cabinets/stats'),
  
  /**
   * 搜索电柜
   * @param {Object} params 搜索参数
   * @returns {Promise}
   */
  searchCabinets: (params) => get('/cabinets/search', params)
}

/**
 * 仓门管理相关API
 */
export const doorAPI = {
  /**
   * 获取电柜仓门列表
   * @param {String|Number} cabinetId 电柜ID
   * @returns {Promise}
   */
  getDoors: (data) => get(`/operator/maintenance/getCabinetDoorList`,data),
  
  /**
   * 获取仓门详情
   * @param {String|Number} cabinetId 电柜ID
   * @param {String|Number} doorId 仓门ID
   * @returns {Promise}
   */
  getDoorDetail: (cabinetId, doorId) => get(`/cabinets/${cabinetId}/doors/${doorId}`),
  
  /**
   * 打开单个仓门
   * @param {String|Number} cabinetId 电柜ID
   * @param {String|Number} doorId 仓门ID
   * @returns {Promise}
   */
  openDoor: (data) => post(`/operator/remote/control/openDoor`, { ...data }),
  
  /**
   * 批量打开仓门
   * @param {String|Number} cabinetId 电柜ID
   * @param {Array} doorIds 仓门ID数组
   * @returns {Promise}
   */
  batchOpenDoors: (data) => post(`/operator/remote/control/openDoor`, { ...data },{
	  header : { 'Content-Type':'application/x-www-form-urlencoded'}
  }),
  
  /**
   * 打开所有仓门
   * @param {String|Number} cabinetId 电柜ID
   * @returns {Promise}
   */
  openAllDoors: (data) => post(`/operator/remote/control/openAllDoor`, { ...data },{
	  header : { 'Content-Type':'application/x-www-form-urlencoded'}
  }),
  
  /**
   * 禁用仓门
   * @param {String|Number} cabinetId 电柜ID
   * @param {String|Number} doorId 仓门ID
   * @returns {Promise}
   */
  disableDoor: (data) => post(`/operator/remote/control/disenableDoor`, { ...data },{
	  header : { 'Content-Type':'application/x-www-form-urlencoded'}
  }),
  
  /**
   * 启用仓门
   * @param {String|Number} cabinetId 电柜ID
   * @param {String|Number} doorId 仓门ID
   * @returns {Promise}
   */
  enableDoor: (data) => post(`/operator/remote/control/enableDoor`, { ...data },{
	  header : { 'Content-Type':'application/x-www-form-urlencoded'}
  }),
  /**
   * 启用维护门
   * @param {String|Number} cabinetId 电柜ID
   * @returns {Promise}
   */
  openMaintenanceDoor: (data) => post(`/operator/remote/control/openMaintenanceDoor`, { ...data },{
  	  header : { 'Content-Type':'application/x-www-form-urlencoded'}
  }),
  /**
   * 获取仓门操作日志
   * @param {String|Number} cabinetId 电柜ID
   * @param {Object} params 查询参数
   * @returns {Promise}
   */
  getDoorLogs: (cabinetId, params = {}) => get(`/cabinets/${cabinetId}/door-logs`, params)
}

/**
 * 电池管理相关API
 */
export const batteryAPI = {
	
	
 /**
   * 获取电池类型列表
   * @param {Object} params 查询参数
   * @returns {Promise}
   */
  getBatteryTypeList: (data = {}) => post('/operator/maintenance/getBatteryTypeList', {...data}),
  
  /**
   * 获取电池列表
   * @param {Object} params 查询参数
   * @returns {Promise}
   */
  getBatteryList: (data = {}) => post('/operator/maintenance/getBatteryList', {...data}),
  
  /**
   * 获取电池详情
   * @param {String|Number} id 电池ID
   * @returns {Promise}
   */
  getBatteryDetail: (data) => get(`/operator/maintenance/getBatteryDetails`, {...data}),
  
  /**
   * 新增电池信息
   * @param {String|Number} id 电池ID
   * @param {Object} data 更新数据
   * @returns {Promise}
   */
  addBattery: (data) => post(`/operator/maintenance/addBattery`, data),
  
  /**
   * 获取电池统计信息
   * @returns {Promise}
   */
  getBatteryStats: () => get('/batteries/stats'),
  
  /**
   * 电池充电状态监控
   * @param {String|Number} id 电池ID
   * @returns {Promise}
   */
  monitorBattery: (id) => get(`/batteries/${id}/monitor`)
}

/**
 * 门店管理相关API
 */
export const storeAPI = {
  /**
   * 获取门店列表
   * @param {Object} params 查询参数
   * @returns {Promise}
   */
  getStoreList: (params = {}) => get('/stores', params),
  
  /**
   * 获取门店详情
   * @param {String|Number} id 门店ID
   * @returns {Promise}
   */
  getStoreDetail: (id) => get(`/stores/${id}`),
  
  /**
   * 添加门店
   * @param {Object} data 门店数据
   * @returns {Promise}
   */
  addStore: (data) => post('/stores', data),
  
  /**
   * 更新门店信息
   * @param {String|Number} id 门店ID
   * @param {Object} data 更新数据
   * @returns {Promise}
   */
  updateStore: (id, data) => put(`/stores/${id}`, data),
  
  /**
   * 删除门店
   * @param {String|Number} id 门店ID
   * @returns {Promise}
   */
  deleteStore: (id) => del(`/stores/${id}`),
  
  /**
   * 获取门店电柜列表
   * @param {String|Number} storeId 门店ID
   * @returns {Promise}
   */
  getStoreCabinets: (storeId) => get(`/stores/${storeId}/cabinets`)
}

/**
 * 统计报表相关API
 */
export const reportAPI = {
  /**
   * 获取运营统计
   * @param {Object} params 查询参数
   * @returns {Promise}
   */
  getOperationStats: (params = {}) => get('/reports/operation', params),
  
  /**
   * 获取换电统计
   * @param {Object} params 查询参数
   * @returns {Promise}
   */
  getExchangeStats: (params = {}) => get('/reports/exchange', params),
  
  /**
   * 获取收入统计
   * @param {Object} params 查询参数
   * @returns {Promise}
   */
  getRevenueStats: (params = {}) => get('/reports/revenue', params),
  
  /**
   * 导出报表
   * @param {Object} data 导出参数
   * @returns {Promise}
   */
  exportReport: (data) => post('/reports/export', data)
}

/**
 * 新增故障维修相关API
 */
export const fixAPI = {
  /**
   * 获取系统配置
   * @returns {Promise}
   */
  getDevicealarm: (params) => get('/operator/devicealarm/list',params),
  
  /**
   * 新增故障维修
   * @param {Object} data 配置数据
   * @returns {Promise}
   */
  addDevicealarm: (data) => post('/operator/devicealarm', data),
  
  /**
   * 获取详情
   * @param {Object} params 查询参数
   * @returns {Promise}
   */
  getDevicealarmDetail: (params = {}) => get(`/operator/devicealarm/${params.id}`, params),
  
  
  /**
   * 删除故障
   * @param {Object} params 查询参数
   * @returns {Promise}
   */
  delDevicealarms: (data = {}) => del(`/operator/devicealarm/${params.ids}`, data),
  
  /**
   * 上传文件
   * @param {Object} formData 表单数据
   * @param {String} filePath 文件路径
   * @returns {Promise}
   */
  uploadFile: (data,path) => uploadFile('/file/upload', data,path)
  
}

// 默认导出所有API
export default {
  auth: authAPI,
  cabinet: cabinetAPI,
  door: doorAPI,
  battery: batteryAPI,
  store: storeAPI,
  report: reportAPI,
  fix: fixAPI
}